package com.example.administrator.liveapp.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.example.administrator.liveapp.R;
import com.example.administrator.liveapp.Xutils3.MyCallBack;
import com.example.administrator.liveapp.Xutils3.XUtils;
import com.example.administrator.liveapp.adapter.CommentTab_1RecyclerViewAdapter;
import com.example.administrator.liveapp.adapter.CommentTab_2RecyclerViewAdapter;
import com.example.administrator.liveapp.adapter.MyArticleRecyclerViewAdapter;
import com.example.administrator.liveapp.bean.MyArticleBean;
import com.example.administrator.liveapp.bean.MyCommentBean;
import com.example.administrator.liveapp.utils.AppUrl;
import com.example.administrator.liveapp.view.FullyLinearLayoutManager;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

/**
 * 我的文章activity
 */

public class MyArticle extends AppCompatActivity implements View.OnClickListener{

    private String consumerId;
    private RecyclerView Article_Recycle;
    private MyArticleBean bean;
    private TextView no_content;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_article);
        initView();
    }

    private void initView() {
        Intent intent = getIntent();
        consumerId = intent.getStringExtra("consumerId");
        Article_Recycle = (RecyclerView) findViewById(R.id.Article_Recycle);
        no_content = (TextView) findViewById(R.id.no_content);
        findViewById(R.id.article_returns).setOnClickListener(this);
        setData();
    }

    private void setData() {
        String url = AppUrl.URL +  AppUrl.ListArticleURL;
        Map<String,String> map = new HashMap<>();
        map.put("consumerId",consumerId);
        XUtils.Get(url,map,new MyCallBack<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Gson gson = new Gson();
                bean = gson.fromJson(result,MyArticleBean.class);
                if(bean.getCode() == 7902){
                    no_content.setText("你还没有写文章!");
                    no_content.setVisibility(View.VISIBLE);
                }
                //my_article_Recycle
                FullyLinearLayoutManager linerLayoutManager = new FullyLinearLayoutManager(MyArticle.this);
                linerLayoutManager.setScrollEnabled(true);
                Article_Recycle.setHasFixedSize(true);
                Article_Recycle.setLayoutManager(linerLayoutManager);
                MyArticleRecyclerViewAdapter tab1 = new MyArticleRecyclerViewAdapter(bean.getArticle());
                Article_Recycle.setAdapter(tab1);
                tab1.setOnItemClickListener(new MyArticleRecyclerViewAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        Intent intent = new Intent();
                        intent.putExtra("consumerId",consumerId);
                        intent.putExtra("articleId",bean.getArticle().get(position).getArticleid());
                        intent.setClass(MyArticle.this, ArticleDetailsPage.class);
                        startActivity(intent);
                        overridePendingTransition(R.anim.zoomin,R.anim.zoomout);
                    }
                });
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.article_returns:
                finish();
                break;
        }
    }
}
